home *** CD-ROM | disk | FTP | other *** search
/ Mac Power 1997 December / MACPOWER-1997-12.ISO.7z / MACPOWER-1997-12.ISO / AMUG / PROGRAMMING / Raven 1.2 Examples.sit / Raven 1.2 Examples / Quill / Source / ResourceCommands.cpp < prev    next >
C/C++ Source or Header  |  1997-08-07  |  8KB  |  329 lines

  1. /*
  2.  *  File:       ResourceCommands.cpp
  3.  *  Summary:       Command objects for manipulating resources.
  4.  *  Written by: Jesse Jones
  5.  *
  6.  *  Copyright ゥ 1996 Jesse Jones. 
  7.  *    For conditions of distribution and use, see copyright notice in ZTypes.h  
  8.  *
  9.  *  Change History (most recent first):    
  10.  *
  11.  *         <->     8/10/96    JDJ        Created
  12.  */
  13.  
  14. #include "ResourceCommands.h"
  15.  
  16. #include <List.h>
  17.  
  18. #include <ZHierarchicalTable.h>
  19. #include <ZHierarchicalTableExtras.h>
  20. #include <ZStringUtils.h>
  21.  
  22. #include "ResourceSubNode.h"
  23.  
  24.  
  25. // ===================================================================================
  26. //    class CCreateResourceCommand
  27. // ===================================================================================
  28.  
  29. //---------------------------------------------------------------
  30. //
  31. // CCreateResourceCommand::~CCreateResourceCommand
  32. //
  33. //---------------------------------------------------------------
  34. CCreateResourceCommand::~CCreateResourceCommand()
  35. {
  36. }
  37.  
  38.  
  39. //---------------------------------------------------------------
  40. //
  41. // CCreateResourceCommand::CCreateResourceCommand
  42. //
  43. //---------------------------------------------------------------
  44. CCreateResourceCommand::CCreateResourceCommand(CResourceSubNode* subNode)
  45. {
  46.     ASSERT(subNode != nil);
  47.     
  48.     mSubNode = subNode;
  49.     mRsrcMap = subNode->GetMap();
  50.  
  51.     mID = mRsrcMap->GetLastResourceID() + 1;
  52. }
  53.  
  54.  
  55. //---------------------------------------------------------------
  56. //
  57. // CCreateResourceCommand::GetText
  58. //
  59. //---------------------------------------------------------------
  60. string CCreateResourceCommand::GetText() const
  61. {
  62.     return LoadIndString(256, 17);
  63. }
  64.  
  65.  
  66. //---------------------------------------------------------------
  67. //
  68. // CCreateResourceCommand::OnDo
  69. //
  70. //---------------------------------------------------------------
  71. void CCreateResourceCommand::OnDo()
  72. {
  73.     this->OnRedo();    
  74. }
  75.  
  76.  
  77. //---------------------------------------------------------------
  78. //
  79. // CCreateResourceCommand::OnUndo
  80. //
  81. //---------------------------------------------------------------
  82. void CCreateResourceCommand::OnUndo()
  83. {
  84.     mRsrcMap->DeleteResource(mID);                
  85. }
  86.  
  87.  
  88. //---------------------------------------------------------------
  89. //
  90. // CCreateResourceCommand::OnRedo
  91. //
  92. //---------------------------------------------------------------
  93. void CCreateResourceCommand::OnRedo()
  94. {
  95.     mRsrcMap->AddResource(SResource(mID, "", mRsrc));                
  96.     
  97.     long index = mSubNode->FindID(mID);    
  98.     TBaseTableNode* node = mSubNode->GetNode(index);
  99.     node->GetTable()->SelectOneNode(node);    
  100. }
  101.  
  102. #pragma mark -
  103.  
  104. // ===================================================================================
  105. //    class CResourceInfoCommand
  106. // ===================================================================================
  107.  
  108. //---------------------------------------------------------------
  109. //
  110. // CResourceInfoCommand::~CResourceInfoCommand
  111. //
  112. //---------------------------------------------------------------
  113. CResourceInfoCommand::~CResourceInfoCommand()
  114. {
  115. }
  116.  
  117.  
  118. //---------------------------------------------------------------
  119. //
  120. // CResourceInfoCommand::CResourceInfoCommand
  121. //
  122. //---------------------------------------------------------------
  123. CResourceInfoCommand::CResourceInfoCommand(CResourceNode* node, ResID oldID, const string& oldName, ResID newID, const string& newName)
  124. {
  125.     ASSERT(node != nil);
  126.     ASSERT(oldID != newID || oldName != newName);
  127.     
  128.     mNode    = node;
  129.     mRsrcMap = node->GetMap();
  130.     
  131.     mOldID   = oldID;
  132.     mOldName = oldName;
  133.     
  134.     mNewID   = newID;
  135.     mNewName = newName;
  136. }
  137.  
  138.  
  139. //---------------------------------------------------------------
  140. //
  141. // CResourceInfoCommand::GetText
  142. //
  143. //---------------------------------------------------------------
  144. string CResourceInfoCommand::GetText() const
  145. {
  146.     if (mOldID != mNewID && mOldName == mNewName)
  147.         return LoadIndString(256, 20);
  148.             
  149.     else if (mOldName != mNewName && mOldID == mNewID)
  150.         return LoadIndString(256, 21);
  151.         
  152.     else
  153.         return LoadIndString(256, 22);
  154. }
  155.  
  156.  
  157. //---------------------------------------------------------------
  158. //
  159. // CResourceInfoCommand::OnDo
  160. //
  161. //---------------------------------------------------------------
  162. void CResourceInfoCommand::OnDo()
  163. {
  164.     this->OnRedo();
  165. }
  166.  
  167.  
  168. //---------------------------------------------------------------
  169. //
  170. // CResourceInfoCommand::OnUndo
  171. //
  172. //---------------------------------------------------------------
  173. void CResourceInfoCommand::OnUndo()
  174. {
  175.     if (mOldID != mNewID && mOldName == mNewName)
  176.         mRsrcMap->SetResourceID(mNewID, mOldID);    
  177.             
  178.     else if (mOldName != mNewName && mOldID == mNewID)
  179.         mRsrcMap->SetResourceName(mOldID, mOldName);    
  180.         
  181.     else {
  182.         mRsrcMap->SetResourceID(mNewID, mOldID);    
  183.         mRsrcMap->SetResourceName(mOldID, mOldName);    
  184.     }
  185.     
  186.     mNode->GetTable()->SelectOneNode(mNode);    
  187. }
  188.  
  189.  
  190. //---------------------------------------------------------------
  191. //
  192. // CResourceInfoCommand::OnRedo
  193. //
  194. //---------------------------------------------------------------
  195. void CResourceInfoCommand::OnRedo()
  196. {
  197.     if (mOldID != mNewID && mOldName == mNewName)
  198.         mRsrcMap->SetResourceID(mOldID, mNewID);    
  199.             
  200.     else if (mOldName != mNewName && mOldID == mNewID)
  201.         mRsrcMap->SetResourceName(mOldID, mNewName);    
  202.         
  203.     else {
  204.         mRsrcMap->SetResourceID(mOldID, mNewID);    
  205.         mRsrcMap->SetResourceName(mNewID, mNewName);    
  206.     }
  207.     
  208.     mNode->GetTable()->SelectOneNode(mNode);    
  209. }
  210.  
  211. #pragma mark -
  212.  
  213. // ===================================================================================
  214. //    class CDuplicateResourceCommand
  215. // ===================================================================================
  216.  
  217. //---------------------------------------------------------------
  218. //
  219. // CDuplicateResourceCommand::~CDuplicateResourceCommand
  220. //
  221. //---------------------------------------------------------------
  222. CDuplicateResourceCommand::~CDuplicateResourceCommand()
  223. {
  224.     delete mNewData;
  225. }
  226.  
  227.  
  228. //---------------------------------------------------------------
  229. //
  230. // CDuplicateResourceCommand::CDuplicateResourceCommand
  231. //
  232. //---------------------------------------------------------------
  233. CDuplicateResourceCommand::CDuplicateResourceCommand(THierarchicalTable* table)
  234. {
  235.     ASSERT(table != nil);
  236.     ASSERT(table->HasSelection());
  237.     
  238.     mTable = table;
  239.     
  240.     mNewData = new list<SEntry, allocator<SEntry> >;
  241.         
  242.     short count = 1;
  243.  
  244.     THierarchicalIter<CResourceNode> iter(mTable);
  245.     while (iter) {
  246.         CResourceNode* node = *iter;
  247.         ++iter;
  248.         
  249.         if (node->IsSelected()) {
  250.             node->UpdateResource();
  251.  
  252.             SEntry entry;
  253.             entry.rsrc = node->GetResource();
  254.             entry.node = node;
  255.             
  256.             entry.rsrc.data.Detach();
  257.             entry.rsrc.id = node->GetMap()->GetLastResourceID() + count;
  258.             
  259.             mNewData->push_back(entry);
  260.             
  261.             count++;
  262.         }
  263.     }
  264. }
  265.  
  266.  
  267. //---------------------------------------------------------------
  268. //
  269. // CDuplicateResourceCommand::GetText
  270. //
  271. //---------------------------------------------------------------
  272. string CDuplicateResourceCommand::GetText() const
  273. {
  274.     if (mNewData->size() > 1)
  275.         return LoadIndString(256, 18);
  276.     else        
  277.         return LoadIndString(256, 18);
  278. }
  279.  
  280.  
  281. //---------------------------------------------------------------
  282. //
  283. // CDuplicateResourceCommand::OnDo
  284. //
  285. //---------------------------------------------------------------
  286. void CDuplicateResourceCommand::OnDo()
  287. {    
  288.     this->OnRedo();
  289. }
  290.  
  291.  
  292. //---------------------------------------------------------------
  293. //
  294. // CDuplicateResourceCommand::OnUndo
  295. //
  296. //---------------------------------------------------------------
  297. void CDuplicateResourceCommand::OnUndo()
  298. {
  299.     list<SEntry, allocator<SEntry> >::iterator iter = mNewData->begin();
  300.     while (iter != mNewData->end()) {
  301.         SEntry entry = *iter++;
  302.         entry.node->GetMap()->DeleteResource(entry.rsrc.id);                
  303.     }
  304. }
  305.  
  306.  
  307. //---------------------------------------------------------------
  308. //
  309. // CDuplicateResourceCommand::OnRedo
  310. //
  311. //---------------------------------------------------------------
  312. void CDuplicateResourceCommand::OnRedo()
  313. {
  314.     mTable->SelectAll(false);
  315.  
  316.     list<SEntry, allocator<SEntry> >::iterator iter = mNewData->begin();
  317.     while (iter != mNewData->end()) {
  318.         SEntry entry = *iter++;
  319.         entry.node->GetMap()->AddResource(entry.rsrc);                
  320.  
  321.         CResourceSubNode* subNode = dynamic_cast<CResourceSubNode*>(entry.node->GetParent());
  322.         long index = subNode->FindID(entry.rsrc.id);    
  323.         TBaseTableNode* node = subNode->GetNode(index);
  324.         node->Select();
  325.     }
  326. }
  327.  
  328.             
  329.